/*
 * @Author: 肖思汗 
 * @Date: 2025-06-11 14:50:04 
 * @Last Modified by: 肖思汗
 * @Last Modified time: 2025-07-01 23:07:41
 */


import { autorun, makeAutoObservable } from "mobx";
import myDesignApi from "./myDesignApi";
import userStore from "src/pages/User/userStore";
import { MyDesignData } from "types/type";

// 我的设计
class MyDesignStore {
    constructor() {
        makeAutoObservable(this, {}, { deep: false });
        // autorun(() => {
        //     if (userStore.isLogin) {
        //         this.getList();
        //     }
        // });
    }

    loading: boolean = false;
    page: number = 1;//当前页面
    pageSize: number = 10;//分页数
    total: number = 0;//总数
    list: Array<MyDesignData> = [];//数据列表
    hasMore: boolean = true;//是否有更多数据

    //获取数据
    getNextPage(reset: boolean = false) {
        if (reset) {
            this.page = 1;
            this.hasMore = true;
        } else if (!this.hasMore || this.loading) {
            return;
        } else {
            this.page++;
        }
        this.loading = true;
        myDesignApi.getList(this.page, this.pageSize).then(async res => {
            res.rows.forEach((item: any) => {
                item.productDesign = JSON.parse(item.productDesign);
            });
            if (reset) {
                this.list = [];
                await new Promise(resolve => requestAnimationFrame(resolve));
                await new Promise(resolve => requestAnimationFrame(resolve));
                await new Promise(resolve => requestAnimationFrame(resolve));
                this.list = [...res.rows];
            } else {
                this.list = [...this.list, ...res.rows];
            }
            this.total = res.total;
            this.hasMore = (res.rows.length === this.pageSize);
            this.loading = false;
        });
    }

    //新增数据
    add(data: any) {

        // this.getList();
    }

    isManaging: boolean = false;
    selectedIds: number[] = [];

    // 切换管理状态
    toggleManage() {
        this.isManaging = !this.isManaging;
        if (!this.isManaging) {
            this.selectedIds = [];
        }
    }

    // 切换选中状态
    toggleSelect(id: number) {
        const index = this.selectedIds.indexOf(id);
        if (index === -1) {
            this.selectedIds = [...this.selectedIds, id];
        } else {
            this.selectedIds.splice(index, 1);
            this.selectedIds = [...this.selectedIds];
        }
    }

    //删除数据
    delete(id?: string) {
        const ids = id ? [id] : this.selectedIds;
        console.log('Deleting IDs:', ids);
        if (ids.length > 0) {
            myDesignApi.delete(ids.join(',')).then(() => {
                this.getNextPage(true);
            });
        }
        this.selectedIds = [];
        this.isManaging = false;
    }

    //更新数据
    updata(data: MyDesignData) {
        myDesignApi.edit(data).then(() => {
            const index = this.list.findIndex(item => item.id === data.id);
            if (index !== -1) {
                this.list[index] = data;
                console.log(this.list[index]);
            }
            this.list = [...this.list];
        });
    }

}

const myDesignStore = new MyDesignStore();

export default myDesignStore;